package com.medical.admin.cost.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.cost.common.dto.*;
import com.medical.admin.patient.entity.PatientCard;
import com.medical.admin.cost.entity.Recharge;
import com.medical.admin.patient.mapper.PatientCardMapper;
import com.medical.admin.cost.mapper.RechargeMapper;
import com.medical.admin.cost.service.IndentService;
import com.medical.admin.patient.service.PatientService;
import com.medical.admin.cost.service.RechargeService;
import com.medical.admin.patient.common.dto.PatientDto;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OutpatientRechargeServiceImpl implements RechargeService {
    @Resource
    private RechargeMapper rechargeMapper;
    @Resource
    private PatientCardMapper patientCardMapper;
    @Resource
    private PatientService patientService;
    @Resource
    private IndentService indentService;

    @Override
    public IPage<OutpatientRechargeInfo> searchList(OutpatientRechargeInfo outpatientRechargeInfo, Integer page, Integer size) {
        List<OutpatientRechargeInfo> list = rechargeMapper.selectListByInfo(outpatientRechargeInfo);
        if (ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseCode.RECHARGE_SELECT_ERROR);
        }
        return listToPage(list,page,size);
    }
    @Transactional
    @Override
    public Integer addRecharge(RechargeDto rechargeDto) {
        int count = rechargeMapper.insert(BeanConvertUtils.convertTo(rechargeDto, Recharge::new));
        if (count <= 0){
            throw new ServiceException(ResponseCode.RECHARGE_ADD_ERROR);
        }
        PatientCard patientCard = patientCardMapper.selectById(rechargeDto.getPatientCardNum());
        if (ObjectUtils.isEmpty(patientCard)){
            throw new ServiceException(ResponseCode.RECHARGE_SELECT_ERROR);
        }
        patientCard.setBalance(patientCard.getBalance()+rechargeDto.getRechargeCount());
        count = patientCardMapper.updateById(patientCard);
        if (count <= 0){
            throw new ServiceException(ResponseCode.BALANCE_UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Integer removeRecharge(Integer rechargeId) {
        int count = rechargeMapper.deleteById(rechargeId);
        if (count <= 0){
            throw new ServiceException(ResponseCode.RECHARGE_DELETE_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyRecharge(RechargeDto rechargeDto) {
        int count = rechargeMapper.updateById(BeanConvertUtils.convertTo(rechargeDto, Recharge::new));
        if (count <= 0){
            throw new ServiceException(ResponseCode.RECHARGE_UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public RechargeDto detailRecharge(Integer rechargeId) {
        Recharge recharge = rechargeMapper.selectById(rechargeId);
        if (ObjectUtils.isEmpty(recharge)){
            throw new ServiceException(ResponseCode.RECHARGE_SELECT_ERROR);
        }
        return BeanConvertUtils.convertTo(recharge, RechargeDto::new);
    }

    @Override
    public DetailInfo outpatientDetailRecharges(Long patientId) {
        PatientDto patientDto = patientService.researchPatient(patientId);
        if(ObjectUtils.isEmpty(patientDto)){
            throw new ServiceException(ResponseCode.PATIENT_NULL_ERROR);
        }
        if (patientDto.getState() ==0){
            throw new ServiceException(ResponseCode.PATIENT_STATE_ERROR);
        }
        IndentDto indentDto = indentService.research(patientId);
        if (ObjectUtils.isEmpty(indentDto)){
            throw new ServiceException(ResponseCode.INDENT_SELECT_ERROR);
        }
        DetailInfo detailInfo = new DetailInfo();
        detailInfo.setPatientDto(patientDto);
        detailInfo.setIndentDto(indentDto);
        return detailInfo;
    }

    public static IPage<OutpatientRechargeInfo> listToPage(List list, int pageNum, int pageSize){
        List pageList = new ArrayList<>();
        int curIdx = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && curIdx + i < list.size(); i++) {
            pageList.add(list.get(curIdx + i));
        }
        IPage<OutpatientRechargeInfo> page = new Page<>(pageNum, pageSize);
        page.setRecords(pageList);
        page.setTotal(list.size());
        return page;
    }
}
